<?php

namespace Biz\model\brandIndex;

use Doraemon\pockets\datebase\BaseDbModel;

class BrandQuestions extends BaseDbModel
{

    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'hhz_store';

    const _TABLE_NAME = 'brand_questions';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;
    const STATUS = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_DELETE => '删除'
    ];

    const DEFAULT_PAGESIZE = 20;

    public static function getQuestions($type = 1)
    {
        return self::select('*', [
            "question_status" => self::STATUS_NORMAL,
            "category_type" => $type,
            "ORDER" => [
                "question_sort" => "ASC",
                "addtime" => "DESC"
            ]
        ]);
    }

    public static function getQuestionsByType($start, $page_size = self::DEFAULT_PAGESIZE, $type = 1)
    {
        return self::select('*', [
            "question_status" => self::STATUS_NORMAL,
            "category_type" => $type,
            "ORDER" => [
                "question_sort" => "ASC",
                "addtime" => "DESC"
            ],
            "LIMIT" => [$start, $page_size]
        ]);
    }

    public static function getQuestionsCountByType($type = 1)
    {
        return self::count([
            "question_status" => self::STATUS_NORMAL,
            "category_type" => $type,
            "ORDER" => [
                "question_sort" => "ASC",
                "addtime" => "DESC"
            ]
        ]);
    }

    //添加
    public static function addData($data)
    {
        self::insert($data);
        return self::id();
    }

    //修改
    public static function editData($data, $where = [])
    {
        if(!$where) {
            return false;
        }
        return self::update($data, $where);
    }


    public static function getQuestionsById($id)
    {
        return self::get('*', [
            "id" => $id,
            "question_status" => self::STATUS_NORMAL
        ]);
    }


    public static function searchByKeyword($keyword = "", $search_content = true)
    {
        if(!$keyword) {
            return [];
        }
        if($search_content) {
            //搜内容
            return self::select("*", [
                "question_status" => self::STATUS_NORMAL,
                "answer_content[~]" => $keyword
            ]);
        } else {
            //搜标题
            return self::select("*", [
                "question_status" => self::STATUS_NORMAL,
                "question_content[~]" => $keyword
            ]);
        }
    }


}
